const { BASE_URL, PIC_URL } = require("../../../config")
const { replaceBaseUrl } = require("../../../utils/util")

let timer = null
// pkg/pages/prodDetail/prodDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showShare: false,
    options: [
      { name: '微信', icon: 'wechat', openType: 'share' },
      { name: '微博', icon: 'weibo' },
      { name: '复制链接', icon: 'link' },
      { name: '分享海报', icon: 'poster' },
      { name: '二维码', icon: 'qrcode' },
    ],
    prodItem: {}
  },
  async onSubmit() {
    const token = wx.getStorageSync('token')
    const {data:res} = await wx.p.request({
      url: BASE_URL + `/order/commit/${this.data.prodItem.id}`,
      method: 'POST',
      header: {
        token: token
      }
    })
    const order = res.data
    wx.showModal({
      title: '确认操作',
      content: `生成订单，编号为${order.orderNumber}\n是否继续支付，请在15分钟内完成订单`,
      showCancel: true,
      cancelText: '取消',
      confirmText: '确认',
      icon: 'none',
      success: async function (res) {
          if (res.confirm) {
              // 支付
              const {data:res} = await wx.p.request({
                url: BASE_URL + `/order/pay/${order.id}`,
                method: 'PUT',
                header: {
                  token: token
                }
              })
              if (res.data.code !== 200) {
                return wx.showModal({
                  title: '支付结果',
                  content: `${res.data}`
                })
              }
              return wx.showModal({
                title: '支付结果',
                content: `${res.data}`
              })
          } else if (res.cancel) {
              return
          }
      }
  });
  },
  async _getProdDetail(id) {
    const that = this
    const token = wx.getStorageSync('token')
    const {data:res} = await wx.p.request({
      url: BASE_URL+`/items/info/${id}`,
      method: 'POST',
      header: {
        token: token
      }
    })
    console.log(res.data);
    // images 
    const images = res.data.images.map(item => {
      return replaceBaseUrl(PIC_URL, item)
    })
    // info
    const info = res.data.info.map(item => {
      return replaceBaseUrl(PIC_URL, item)
    })
    that.setData({
      prodItem: {
        ...res.data,
        images: images,
        info: info
      }
    })

  },
  showShareSheet() {
    this.setData({
      showShare: true
    })
  },
  onClose() {
    this.setData({
      showShare: false
    })
  },
  onSelect(event) {
    console.log(event.detail.name);
    this.onClose();
  },
  toLocate() {
    console.log("百度地图");
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._getProdDetail(options.itemId)
    const token = wx.getStorageSync('token')
    timer = setTimeout(() => {
      wx.request({
        url: BASE_URL+`/behavior/item_view/${options.itemId}`,
        method: 'POST',
        header: {
          token: token
        },
        success(res) {
        }
      })
    }, 6000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})